diff --git a/sherpa/nodejs.go b/sherpa/nodejs.go index 527c212..bfc5d5c 100644 --- a/sherpa/nodejs.go +++ b/sherpa/nodejs.go @@ -23,11 +23,15 @@ import ( "path/filepath" ) -// NodeJSMainModule returns the name of the main module as defined in /package.json. +// NodeJSMainModule returns the name of the main module as defined in /package.json. If no package.json exists, +// or the package.json does not include a main entry, value defaults to server.js in line with the behavior of the +// Paketo NodeJS buildpack. func NodeJSMainModule(path string) (string, error) { file := filepath.Join(path, "package.json") in, err := os.Open(file) - if err != nil { + if os.IsNotExist(err) { + return "server.js", nil + } else if err != nil { return "", fmt.Errorf("unable to open %s\n%w", file, err) } defer in.Close() @@ -39,7 +43,7 @@ func NodeJSMainModule(path string) (string, error) { m, ok := raw["main"].(string) if !ok { - return "", fmt.Errorf("no main module defined in %s: %+v", file, raw) + return "server.js", nil } return m, nil diff --git a/sherpa/nodejs_test.go b/sherpa/nodejs_test.go index 277132e..e6f4cf8 100644 --- a/sherpa/nodejs_test.go +++ b/sherpa/nodejs_test.go @@ -17,7 +17,6 @@ package sherpa_test import ( - "fmt" "io/ioutil" "os" "path/filepath" @@ -47,19 +46,20 @@ func testNodeJS(t *testing.T, context spec.G, it spec.S) { Expect(os.RemoveAll(path)).To(Succeed()) }) - context("NodeJSMainModule", func() { - it("returns main module", func() { - Expect(ioutil.WriteFile(filepath.Join(path, "package.json"), []byte(`{ "main": "test-main" }`), 0644)). - To(Succeed()) + it("returns server.js if no package.json exists", func() { + Expect(sherpa.NodeJSMainModule(path)).To(Equal("server.js")) + }) + + it("returns server.js if package.json does not have a main entry", func() { + Expect(ioutil.WriteFile(filepath.Join(path, "package.json"), []byte(`{}`), 0644)).To(Succeed()) - Expect(sherpa.NodeJSMainModule(path)).To(Equal("test-main")) - }) + Expect(sherpa.NodeJSMainModule(path)).To(Equal("server.js")) + }) - it("returns error if no main module defined", func() { - Expect(ioutil.WriteFile(filepath.Join(path, "package.json"), []byte(`{}`), 0644)).To(Succeed()) + it("returns main module", func() { + Expect(ioutil.WriteFile(filepath.Join(path, "package.json"), []byte(`{ "main": "test-main" }`), 0644)). + To(Succeed()) - _, err := sherpa.NodeJSMainModule(path) - Expect(err).To(MatchError(fmt.Errorf("no main module defined in %s: map[]", filepath.Join(path, "package.json")))) - }) + Expect(sherpa.NodeJSMainModule(path)).To(Equal("test-main")) }) }